package cn.dankal.flyaudio.friend;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.classic.common.MultipleStatusView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import api.ChatServiceFactory;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.base.recyclerview.OnRvItemClickListener;
import cn.dankal.basiclib.pojo.BaseListResponse;
import cn.dankal.basiclib.pojo.BaseResponse;
import cn.dankal.basiclib.pojo.JPushUserInfo;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.basiclib.util.SimpleTextChange;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.basiclib.widget.ClearEditText;
import cn.dankal.flyaudio.FlyApplication;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.SearchFriendAdapter;
import cn.dankal.flyaudio.personal.PersonalProfileActivity;
import cn.jpush.im.android.api.ContactManager;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.api.BasicCallback;

public class AddFriendActivity extends BaseActivity implements View.OnClickListener {

    private ClearEditText searchView;

    private SearchFriendAdapter adapter;

    private RecyclerView recyclerView;

    private View addGroupView;

    private View addGroupByFaceView;

    private MultipleStatusView statusView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_friend;
    }

    @Override
    protected void initComponents() {
        addGroupByFaceView = findViewById(R.id.rl_create_group_by_face);
        statusView = findViewById(R.id.multiple_status_view);
        addGroupView = findViewById(R.id.rl_create_group);
        addGroupView.setOnClickListener(this);
        addGroupByFaceView.setOnClickListener(this);
        searchView = findViewById(R.id.et_search);
        recyclerView = findViewById(R.id.recyclerview);
        findViewById(R.id.tv_search).setOnClickListener(this);
        findViewById(R.id.iv_onback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        adapter = new SearchFriendAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        searchView.setOnSearchListener(new ClearEditText.OnSearchListener() {
            @Override
            public void doSearch(String keyword) {
                if (TextUtils.isEmpty(keyword)) return;
                searchFriend(keyword);
            }
        });
        searchView.addTextChangedListener(new SimpleTextChange() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    statusView.setVisibility(View.GONE);
                }
            }
        });
        adapter.setOnRvItemClickListener(new OnRvItemClickListener<JPushUserInfo>() {
            @Override
            public void onItemClick(View v, int position, JPushUserInfo data) {
                Intent intent = new Intent(AddFriendActivity.this, PersonalProfileActivity.class);
                intent.putExtra("userId", data.getUuid());
                intent.putExtra("phone", data.getPhone());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.tv_search == id) {
            statusView.setVisibility(View.VISIBLE);
            statusView.showContent();
            searchFriend(searchView.getText().toString());
        }else if(R.id.rl_create_group_by_face == id){
            ActivityUtils.startActivity(CreateGroupByPasswordActivity.class);
        }else if(R.id.rl_create_group == id){
            ActivityUtils.startActivity(PickContractPersonActivity.class);
        }

    }

    private void searchFriend(String keyword) {
        statusView.showLoading();
        Map<String, Object> params = new HashMap<>();
        params.put("keyword", keyword);
        ChatServiceFactory.searchShop(params)
                .subscribe(new AbstractSubscriber<BaseResponse<BaseListResponse<JPushUserInfo>>>(this) {
                    @Override
                    public void onNext(BaseResponse<BaseListResponse<JPushUserInfo>> baseListResponseBaseResponse) {
                        if (baseListResponseBaseResponse.getData().getList().isEmpty()) {
                            statusView.showEmpty();
                        } else {
                            adapter.addDatas(baseListResponseBaseResponse.getData().getList(), true);
                            statusView.showContent();
                        }
                    }
                });
    }
}
